package com.lz.comm.image

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.view.View
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.databinding.BindingAdapter
import com.lz.comm.R
import com.bumptech.glide.RequestManager
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import java.io.File

/**
 *
 * //占位符
RequestOptions options = new RequestOptions()
.format(DecodeFormat.PREFER_RGB_565)
.placeholder(R.drawable.lib_bg_pbloading)
.error(R.drawable.lib_bg_pbloading)
.fallback(R.drawable.lib_bg_pbloading);

//缓存
options.skipMemoryCache(true);//不内存缓存如验证码之类图片
options.diskCacheStrategy(DiskCacheStrategy.NONE);//关闭磁盘缓存
 *
 */

object ImageLoader {

    private val ID: Int = R.id.comm_bindingTarget

    @BindingAdapter("url")
    @JvmStatic
    fun loadImage(iv: ImageView, url: String) {
        val tag = iv.getTag(ID)
        if (tag is Target<*>) {
            GlideApp.with(iv.context).load(url).into(tag as Target<Drawable>)
        } else {
            iv.setTag(ID, GlideApp.with(iv.context).load(url).into(iv))
        }
    }

    @JvmStatic
    fun loadImageCircle(iv: ImageView, url: String) {

        GlideApp.with(iv).load(url)
    }

    @JvmStatic
    fun load(request: GlideRequest<*>, options: GlideOptions, iv: ImageView) = request.apply(options).into(iv)

    @JvmStatic
    fun load(request: GlideRequest<*>, iv: ImageView) = request.into(iv)

    fun loadImage(iv: ImageView, url: String, options: RequestOptions) {
        val tag = iv.getTag(ID)
        if (tag is Target<*>) {
            GlideApp.with(iv).load(url).apply(options).into(tag as Target<Drawable>)
        } else {
            iv.setTag(ID, GlideApp.with(iv).load(url).apply(options).into(iv))
        }
    }

    /*  --------------     加载资源到控件Drawable, Bitmap   ---------------  */

    fun load(url: String, iv: ImageView): Target<Drawable> = GlideApp.with(iv).load(url).into(iv)

    fun load(url: String, options: GlideOptions, iv: ImageView): Target<Drawable> = GlideApp.with(iv).load(url).apply(options).into(iv)

    fun bitmap(url: String, iv: ImageView): Target<Bitmap> = GlideApp.with(iv).asBitmap().load(url).into(iv)

    fun bitmap(url: String, options: GlideOptions, iv: ImageView): Target<Bitmap> = GlideApp.with(iv).asBitmap().load(url).apply(options).into(iv)

    /*  --------------     加载本地资源    ---------------  */

    fun load(file: File, iv: ImageView) {
        GlideApp.with(iv).load(file).into(iv)
    }

    fun load(file: File, options: GlideOptions, iv: ImageView) {
        GlideApp.with(iv).load(file).apply(options).into(iv)
    }

    fun load(@DrawableRes res: Int, iv: ImageView) {
        GlideApp.with(iv).load(res).into(iv)
    }

    fun load(@DrawableRes res: Int, options: GlideOptions, iv: ImageView) {
        GlideApp.with(iv).load(res).apply(options).into(iv)
    }

    /*  --------------     清除加载    ---------------  */

    fun clear(requests: RequestManager, vararg targetes: Target<*>?) = targetes.forEach { requests.clear(it) }

    fun clear(requests: RequestManager, vararg vs: View) = vs.forEach { requests.clear(it) }

}